package com.ling.algorithm;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 控制多线程的顺序执行
 */
public class SequentialExecution {

    //join
//    public static void main(String[] args) {
//        Thread t1 = new Thread(() -> {
//            System.out.println("Thread T1 is executing");
//        });
//
//        Thread t2 = new Thread(() -> {
//            try {
//                t1.join();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("Thread T2 is executing");
//        });
//
//        Thread t3 = new Thread(() -> {
//            try {
//                t2.join();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("Thread T3 is executing");
//        });
//        t1.start();
//        t2.start();
//        t3.start();
//    }

    //CountDownLatch
//    public static void main(String[] args) {
//        CountDownLatch countDownLatch = new CountDownLatch(3);
//        Thread t1 = new Thread(() -> {
//            System.out.println("Thread T1 is executing");
//            countDownLatch.countDown();
//        });
//
//        Thread t2 = new Thread(() -> {
//            countDownLatch.countDown();
//            System.out.println("Thread T2 is executing");
//        });
//
//        Thread t3 = new Thread(() -> {
//            try {
//                countDownLatch.await();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("Thread T3 is executing");
//        });
//
//
//        t1.start();
//        t2.start();
//        t3.start();
//    }

    //线程池方式
//    public static void main(String[] args) {
//        Thread t1 = new Thread(() -> {
//            System.out.println("Thread T1 is executing");
//        });
//
//        Thread t2 = new Thread(() -> {
//            System.out.println("Thread T2 is executing");
//        });
//
//        Thread t3 = new Thread(() -> {
//            System.out.println("Thread T3 is executing");
//        });
//
//        ExecutorService executor = Executors.newSingleThreadExecutor();
//        executor.submit(t1);
//        executor.submit(t2);
//        executor.submit(t3);
//        executor.shutdown();
//    }

    //CompletableFuture
    public static void main(String[] args) {
        Thread t1 = new Thread(new work(), "t1");
        Thread t2 = new Thread(new work(), "t2");
        Thread t3 = new Thread(new work(), "t3");

        CompletableFuture.runAsync(()-> t1.start()).thenRun(()-> t2.start()).thenRun(()-> t3.start());
    }

    static class work implements Runnable {
        @Override
        public void run() {
            System.out.println("执行："+Thread.currentThread().getName());
        }
    }

}
